<?php

namespace app\manystore\controller;

use app\common\controller\ManystoreBase;

/**
 * 消息通知管理
 *
 * @icon fa fa-circle-o
 */
class Notice extends ManystoreBase
{

    /**
     * Notice模型对象
     * @var \app\manystore\model\Notice
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\manystore\model\Notice;
        $this->view->assign("roleList", $this->model->getRoleList());
        $this->view->assign("readList", $this->model->getReadList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    //消息通知列表
    public function noticelist()
    {
        $notifications = $this->model
            ->where(['role' => 1, 'uid' => $this->auth->id])
            ->order('id desc')
            ->select();
        $this->success('获取成功', '', $notifications);
    }

    //标记已读
    public function markAsRead()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $info = $this->model->where(['id' => $id])->find();
        if (!$info) {
            $this->error('数据读取失败');
        }

        $info->save(['read' => 2]);
        $this->success('已标记为已读');
    }

    //全部已读
    public function markAllAsRead()
    {
        $uid = $this->auth->id;
        $sql = $this->model->where(['role' => 1, 'uid' => $uid])->update(['read' => 2]);
        if (!$sql){
            $this->error('无数据更新');
        }
        $this->success('已标记为已读');
    }



}
